package com.ysb.corrector.common;

import java.util.*;

/**
 * @author husc
 * @date 2021/3/19
 */
public class PriopritySet {
    private int capacity;
    private TreeMap<Float, Item> data;

    public PriopritySet(int capacity) {
        this.capacity = capacity;
        this.data = new TreeMap<>(Comparator.reverseOrder());
    }

    public int size() {
        return data.size();
    }

    public void put(float score, List<String> path) {
        data.put(score, new Item(score, path));
        while (data.size() > capacity) {
            Float lastKey = data.lastKey();
            data.remove(lastKey);
        }
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public TreeMap<Float, Item> getData() {
        return data;
    }

    public List<Item> getItemList() {
        return new LinkedList<>(data.values());
    }

    public void setData(TreeMap<Float, Item> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder("[ \n");
        for (Map.Entry<Float, Item> entry : data.entrySet()) {
            s.append("\t").append(entry.getValue().getPath()).append("\n");
        }
        s.append("]");
        return s.toString();
    }
}
